package com.taobao.home.api;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.taobao.base.model.RestResponse;
import com.taobao.base.model.UserInfo;
import com.taobao.home.model.dto.ChannelDto;
import com.taobao.home.model.po.Goods;
import com.taobao.home.model.po.Process;
import com.taobao.home.service.CommonService;
import com.taobao.home.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 公共接口
 */
@RestController
public class CommonController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/getRecommendChannel")
    public RestResponse<List<ChannelDto>> getRecommendChannel() {

        System.out.println("测试");
        return commonService.getRecommendChannel();
    }
    @GetMapping("/test1")
    public RestResponse<Goods> test1() {

        return commonService.test1();
    }

    @GetMapping("/test2")
    public RestResponse<Goods> test2() {

        return commonService.test2();
    }

    /**
     * 根据access_token获取用户信息
     */
    @GetMapping("/getuserinfo/{access_token}")
    public RestResponse<UserInfo.User> getUserInfo(@PathVariable("access_token") String access_token) {

        System.out.println(access_token);
        UserInfo forObject = restTemplate.getForObject("http://localhost:47781/auth/oauth/check_token?token=" + access_token, UserInfo.class);

        UserInfo.User user = null;
        // 字符串转json
        ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
        String str = forObject.getUser_name();
        System.out.println(str);
        try {
            user = objectMapper.readValue(forObject.getUser_name(), UserInfo.User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return RestResponse.success(user);

    }

//    @GetMapping("/test1")
//    public RestResponse<List<Process>> test1() {
//
//        return processService.test1();
//    }
//
//    @GetMapping("/test2")
//    public RestResponse<String> test2() {
//
//        return processService.test2();
//    }

    @GetMapping("/test3")
    public List<Process> test3() {

        return processService.getProcessList(0, 2, 3);
    }

}
